using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Pg.BioMedics.RatObserver.Media;
using Pg.BioMedics.RatObserver.ImageProcessing;

namespace Pg.BioMedics.RatObserver
{
    public partial class AnalyzeForm : Form
    {
        #region Delegates

        private delegate void SetIntDelegate(int value);

        #endregion

        #region Fields

        private FrameBuffer _FrameBuffer;
        private ImageProcessor _ImageProcessor;

        #endregion

        #region Constructors

        public AnalyzeForm(FrameBuffer frameBuffer)
        {
            InitializeComponent();

            _FrameBuffer = frameBuffer;
            _FrameBuffer.OnChanged += new EventHandler(FrameBuffer_OnChanged);

            _ImageProcessor = new ImageProcessor();
        }

        #endregion

        #region Event handlers

        void FrameBuffer_OnChanged(object sender, EventArgs e)
        {
            if (_FrameBuffer.Count > 0)
                previewPictureBox.Image = _ImageProcessor.Process(_FrameBuffer.Bitmaps[_FrameBuffer.Count - 1]);
            else
                previewPictureBox.Image = null;

            bufferFillProgressBar.Invoke(Delegate.CreateDelegate(typeof(SetIntDelegate), bufferFillProgressBar, "set_Value"), 
                                        (int)(((double)_FrameBuffer.Count / (double)_FrameBuffer.Capacity) * 100.0d));
            bufferFillProgressBar.Refresh();
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            try
            {
                _FrameBuffer.RunActive();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show(this, "Na pewno chcesz przerwac analize?",
                                          "Potwierdzenie", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Hide();
                    AnalizeResultForm resultForm = new AnalizeResultForm();
                    Pg.BioMedics.RatObserver.Database.Model.AnalizeResult result = new Pg.BioMedics.RatObserver.Database.Model.AnalizeResult();
                    result.spinCount = new Random((int)DateTime.Now.Ticks).Next(15);
                    result.spinDirection = new Random((int)DateTime.Now.Ticks).Next(2);

                    resultForm.SetAnalizeResult(result, "");


                    resultForm.ShowDialog();

                    _FrameBuffer.PauseActive();
                    DialogResult = DialogResult.Cancel;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void simpleMotionDetectionRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            _ImageProcessor.MotionDetectionMethod = MotionDetectionMethod.Basic;
        }

        private void adaptiveMotionDetectionRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            _ImageProcessor.MotionDetectionMethod = MotionDetectionMethod.Adaptive;
        }

        #endregion

        

      
    }
}